<?php
namespace admin\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;
use herosphp\core\Loader;

use message\service\SmsService;

Loader::import('message.service.SmsService');
/**--tmpl-use--**/



class RemindSmsAction extends CommonAction {
	private $to_way = null;
	protected $serviceBean = "message.messageRemind.service";

	public function C_start()
	{
		parent::C_start();

		$remindModel = Loader::model('messageRemind');

		$this->to_way = $remindModel::TO_WAY_SMS_REMIND;
	}


	public function index(HttpRequest $request)
	{

		$remindService = Beans::get($this->serviceBean);

		$page = $request->getParameter('page');
		$pagesize = $request->getParameter('pagesize');

		if ($page <= 0) $page = 1;
		if ($pagesize <= 0) $pagesize = 15;
		$this->page=$page;
		$this->pagesize=$pagesize;


		$search_arr = array(
			"select_mix_option1" => $request->getParameter('select_mix_option1'),
			"select_mix_option_value1" => urldecode($request->getParameter('select_mix_option_value1')),
			"select_mix_option2" => $request->getParameter('select_mix_option2'),
			"start_time" => $request->getParameter('start_time'),
			"end_time" => $request->getParameter('end_time'),
			"manager_name" => urldecode($request->getParameter('manager_name')),
			"to_way" => $this->to_way
		);


		$arr = $remindService->search($pagesize, $page, $search_arr);

		$items = $arr['items'];

		$itemsLen = count($items);

		for ($i = 0; $i < $itemsLen; $i++) {
			if ($items[$i]['manager_name'] == '') {

				$items[$i]['manager_name'] = 'system';
			}
			$items[$i]['to_status_text'] = $remindService->getRemindToStatusText($items[$i]['to_status']);

		}

		$this->generatePageMenu($arr['total']);
		$this->assign('items', $items);
		$this->assign('search_arr', $search_arr);

		$this->assign("title", "短信列表");
		$this->assign('bread', array('网站设置',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;','javascript:;'));
		$this->setView(strtolower($request->getAction()) . '/index');


	}

	public function add(HttpRequest $request)
	{
		$remindService = Beans::get($this->serviceBean);

		$to_member_idoptions = Loader::model('member')->getItems(array('del_status'=>0));

		$option_arr = $remindService->getRemindTmpls($this->to_way);

		$this->assign('option_arr', $option_arr);
		$this->assign('to_member_idoptions', $to_member_idoptions);

		$this->assign("title", "添加短信");
		$this->assign('bread', array('网站设置','短信列表',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;',url('admin/remindSms/index'),'javascript:;'));
		$this->setView(strtolower($request->getAction()) . '/add');

	}


	public function do_add(HttpRequest $request)
	{
		$memberService = Beans::get('member.member.service');

		$remindService = Beans::get($this->serviceBean);
		$remindModel = Loader::model('messageRemind');

		$data = $request->getParameter('data');
		$item = $remindService->getRemindTmplById($data['tmpl_id']);
		$data['tmpl_name'] = $item['name'];
		$data['to_time'] = myNow();
		$data['to_way'] = $this->to_way;

		$member = $memberService->getUsernameById($data['to_member_id']);
		$data['to_member_moblie']=$member['username'];

	    $smsService = new SmsService();

		if($smsService->send($member['username'], $data['to_content'])){//发送短信

			$data['to_status']=1;
		}else{

			$data['to_status']=2;
		}

		$remindModel->add($data) ? AjaxResult::ajaxSuccessResult() : AjaxResult::ajaxFailtureResult();

	}
    //重发
	public function do_retransmission(HttpRequest $request){


		$remindModel = Loader::model('messageRemind');
		$smsService = new SmsService();

		$id = $request->getParameter('id');

		$item=$remindModel->getItem($id);


		if($smsService->send($item['to_member_moblie'], $item['to_content'])){//发送短信

			$data['to_status']=1;
		}else{

			$data['to_status']=2;
		}

		$remindModel->update($data,$id) ? AjaxResult::ajaxSuccessResult() : AjaxResult::ajaxFailtureResult();

	}


	//根据id获取短信
	public function getRemindById(HttpRequest $request)
	{

		$remindService = Beans::get($this->serviceBean);

		$item = $remindService->getRemindById($request->getParameter('id'));
		if($item['manager_name']==''){
			$item['manager_name']='System';
		}
		$item ? AjaxResult::ajaxResult('000', 'ok', $item) : AjaxResult::ajaxResult('001', 'no', $item);

	}


	//规则列表
	public function ruleIndex(HttpRequest $request)
	{

		$remindService = Beans::get($this->serviceBean);

		$page = $request->getParameter('page');
		$pagesize = $request->getParameter('pagesize');

		if ($page <= 0) $page = 1;
		if ($pagesize <= 0) $pagesize = 15;
		$this->page=$page;
		$this->pagesize=$pagesize;
		$search_arr = array(
			"select_mix_option1" => $request->getParameter('select_mix_option1'),
			"select_mix_option_value1" => urldecode($request->getParameter('select_mix_option_value1')),
			"select_mix_option2" => $request->getParameter('select_mix_option2'),
			"start_time" => $request->getParameter('start_time'),
			"end_time" => $request->getParameter('end_time'),
			"trigger_node" => $request->getParameter('trigger_node'),
			"status" => $request->getParameter('status'),
			"to_way" => $this->to_way

		);

		$arr = $remindService->searchByRule($pagesize, $page, $search_arr);

		$items = $arr['items'];

		$itemsLen = count($items);

		for ($i = 0; $i < $itemsLen; $i++) {

			$items[$i]['status'] = $remindService->getRemindRuleStatusText($items[$i]['status']);

			$items[$i]['trigger_node'] = $remindService->getRemindRuleTriggerNodeText($items[$i]['trigger_node']);
		}
		$rule_option_arr = $remindService->getRemindRuleTriggerNodeText();

		$this->assign('rule_option_arr', $rule_option_arr);
		$this->generatePageMenu($arr['total']);
		$this->assign('items', $items);
		$this->assign('search_arr', $search_arr);

		$this->assign("title", "规则列表");
		$this->assign('bread', array('网站设置',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;','javascript:;'));
		$this->setView(strtolower($request->getAction()) . '/indexRule');
	}

	//添加规则
	public function ruleAdd(HttpRequest $request)
	{
		$remindService = Beans::get($this->serviceBean);

		$tmpl_option_arr = $remindService->getRemindTmpls($this->to_way);

		$rule_option_arr = $remindService->getRemindRuleTriggerNodeText();


		$this->assign('tmpl_option_arr', $tmpl_option_arr);
		$this->assign('rule_option_arr', $rule_option_arr);

		$this->assign("title", "添加规则");
		$this->assign('bread', array('网站设置','规则列表',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;',url('admin/remindSms/ruleIndex'),'javascript:;'));
		$this->setView('/remindsms/add_rule');


	}


	public function editRule(HttpRequest $request)
	{
		$remindService = Beans::get($this->serviceBean);

		$tmpl_option_arr = $remindService->getRemindTmpls($this->to_way);
		$rule_option_arr = $remindService->getRemindRuleTriggerNodeText();
		$statusList = $remindService->getRemindRuleStatusText();

		$item = Loader::model('messageRemindRule')->getItem(array('id' => $request->getParameter('id')));
		$tmplItem = $remindService->getRemindTmplById($item['tmpl_id']);
		$item['to_content'] = $tmplItem['content'];
		$this->assign("item", $item);
		$this->assign("statusList", $statusList);
		$this->assign("tmpl_option_arr", $tmpl_option_arr);
		$this->assign("rule_option_arr", $rule_option_arr);

		$this->assign("title", "编辑规则");
		$this->assign('bread', array('网站设置','规则列表',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;',url('admin/remindSms/ruleIndex'),'javascript:;'));
		$this->setView('/remindsms/edit_rule');

	}

	//添加规则
	public function do_addRule(HttpRequest $request)
	{
		$triggerNodeService = new TriggerNodeService();

		$data = $request->getParameter('data');

		if (!$triggerNodeService->validateTrggerNode($data['trigger_node'], $data['to_content'])) {

			AjaxResult::ajaxResult(1, '节点对应的模板里标签不匹配');
		}

		$data['addtime'] = myNow();
		$data['manager_id'] = adminId();
		$data['to_way'] = $this->to_way;

		$bool = Loader::model('messageRemindRule')->add($data);

		$bool ? AjaxResult::ajaxSuccessResult() : AjaxResult::ajaxFailtureResult();

	}

	//编辑规则
	public function do_editRule(HttpRequest $request)
	{

		$data = $request->getParameter('data');
		$id = $request->getParameter('id');

		$bool = Loader::model('messageRemindRule')->update($data, $id);

		$bool ? AjaxResult::ajaxSuccessResult() : AjaxResult::ajaxFailtureResult();
	}


	//模板列表
	public function tmplIndex(HttpRequest $request)
	{
		$remindService = Beans::get($this->serviceBean);

		$page = $request->getParameter('page');
		$pagesize = $request->getParameter('pagesize');

		if ($page <= 0) $page = 1;
		if ($pagesize <= 0) $pagesize = 15;
		$this->page=$page;
		$this->pagesize=$pagesize;

		$search_arr = array(
			"select_mix_option1" => $request->getParameter('select_mix_option1'),
			"select_mix_option_value1" => urldecode($request->getParameter('select_mix_option_value1')),
			"select_mix_option2" => $request->getParameter('select_mix_option2'),
			"start_time" => $request->getParameter('start_time'),
			"end_time" => $request->getParameter('end_time'),
			"status" => $request->getParameter('status'),
			"manager_name" => urldecode($request->getParameter('manager_name')),
			"to_way" => $this->to_way


		);
		$arr = $remindService->searchByTmpl($pagesize, $page, $search_arr);

		$items = $arr['items'];

		$itemsLen = count($items);

		for ($i = 0; $i < $itemsLen; $i++) {

			$items[$i]['status'] = $remindService->getRemindTmplStatusText($items[$i]['status']);

		}
		$this->generatePageMenu($arr['total']);

		$this->assign('items', $items);
		$this->assign('search_arr', $search_arr);

		$this->assign("title", "模板列表");
		$this->assign('bread', array('网站设置',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;','javascript:;'));
		$this->setView(strtolower($request->getAction()) . '/indexTmpl');


	}

	//模板添加
	public function tmplAdd(HttpRequest $request)
	{

		$this->assign("title", "添加模板");
		$this->assign('bread', array('网站设置','模板列表',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;',url('admin/remindSms/tmplIndex'),'javascript:;'));
		$this->setView('/remindsms/add_tmpl');
	}

	public function editTmpl(HttpRequest $request)
	{
		$remindService = Beans::get($this->serviceBean);

		$item = Loader::model('messageRemindTmpl')->getItem(array('id' => $request->getParameter('id')));

		$statusList = $remindService->getRemindTmplStatusText();

		$this->assign("item", $item);
		$this->assign("statusList", $statusList);

		$this->assign("title", "编辑模板");
		$this->assign('bread', array('网站设置','模板列表',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;',url('admin/remindSms/tmplIndex'),'javascript:;'));
		$this->setView('/remindsms/edit_tmpl');

	}

	//添加模板
	public function do_addTmpl(HttpRequest $request)
	{
		$data = $request->getParameter('data');
		$data['to_way'] = $this->to_way;

		$bool = Loader::model('messageRemindTmpl')->add($data);

		$bool ? AjaxResult::ajaxSuccessResult() : AjaxResult::ajaxFailtureResult();
	}

	//编辑模板
	public function do_editTmpl(HttpRequest $request)
	{
		$data = $request->getParameter('data');
		$id = $request->getParameter('id');

		if($data['status']==0&&Loader::model('messageRemindRule')->getItem(array('status'=>1,'tmpl_id'=>$id))){

			AjaxResult::ajaxResult('001','有规则在使用此模板，无法禁用');

		}
		$bool = Loader::model('messageRemindTmpl')->update($data, $id);

		$bool ? AjaxResult::ajaxSuccessResult() : AjaxResult::ajaxFailtureResult();

	}

	//获取模板信息
	public function getRemindTmplById(HttpRequest $request)
	{

		$remindService = Beans::get($this->serviceBean);

		$item = $remindService->getRemindTmplById($request->getParameter('id'));

		$item ? AjaxResult::ajaxResult('000', 'ok', $item) : AjaxResult::ajaxResult('001', 'no', $item);

	}

	//发送测试短信
	public function testSendSms(HttpRequest $request)
	{


		$user = $request->getParameter('accout');
		$pass = $request->getParameter('password');
		$content = $request->getParameter('test_content');
		$mobile = $request->getParameter('test_mobile');

		$provider = ProviderFactory::getProvider("Cr6868");
		$provider->auth($user, $pass, '', '');
		$provider->send($mobile, $content) ? AjaxResult::ajaxSuccessResult() : AjaxResult::ajaxFailtureResult();

	}




}
